<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <link rel="stylesheet" href="/static/style.css">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
<div class="vote-add">
    <form class="layui-form layui-form-pane" lay-filter="addForm" action="">
        <div class="layui-form-item" pane>
            <label class="layui-form-label">工号</label>
            <div class="layui-input-block">
                <input type="text" name="jobNumber" lay-verType="tips" required lay-verify="required|number" placeholder="请输入工号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verType="tips" required lay-verify="required" placeholder="用户名" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">学院</label>
            <div class="layui-input-block">
                <input type="text" name="academy" lay-verType="tips" required lay-verify="required" placeholder="所在学院" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="true" title="男">
                <input type="radio" name="sex" value="false" title="女" checked>
            </div>
        </div>
        <div class="layui-form-item layui-inline" pane>
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="role" id="">
                    <option value="user" selected>用户</option>
                    <option value="admin">管理员</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center">
                <button class="layui-btn" type="button" lay-submit lay-filter="addFormBtn">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/static/layui/layui.js"></script>
<script>
    layui.extend({
        voteList: '/static/lib/userList'
    });
    layui.use(['form', 'jquery', 'laydate', 'layer'], function () {
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,laydate = layui.laydate;

        //表单验证
        form.verify({
            jobNumber: function (v, i) {
                if (v.length != 8){
                    return '长度要为8位'
                }
            }
        });

        //为表单设置初始值
        form.val("addForm", {
            "voteLimit": "5"
            ,"voteForOne": "1"
            ,"isSelfVoted": false
        });

        //监听提交
        form.on('submit(addFormBtn)', function (data) {

            var field = data.field;
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            $.ajax({
                url: '/user'
                ,type: 'POST'
                ,contentType: 'application/json'
                ,data: JSON.stringify(field)
                ,success: function (data) {
                    if (data.status == 200){
                        parent.layui.table.reload('userList',{page: {curr: 1}}); //重载表格
                        layer.confirm(data.msg, {
                            btn: ['确定']
                        }, function () {
                            parent.layer.close(index);
                        })
                    } else {
                        layer.confirm(data.msg, {
                            btn: ['确定']
                        });
                    }

                }
            });
            return false;
        });
    });
</script>
</body>
</html>